<template>
	<view class="t-index-address">
		<scroll-view class="t-index-address__scroll-view" :scroll-into-view="scrollview" :scroll-y="true"
			:enable-flex="true">
			<view :id="group.initial" v-for="group in cityList" :key="group.initial">
				<view class="t-index-address__anchor">
					<text>{{ group.initial }}</text>
				</view>

				<view class="new-address-layout">
					<view v-for="(city, index) in group.list" :key="index" @click="clickCity(city)">
						<view class="cityText_choose " v-if="lastClickCode==city.code">{{ city.name }}</view>
						<view class="cityText_unchoose " v-else>{{ city.name }}</view>
					</view>
				</view>

			</view>
		</scroll-view>

		<view class="t-index-address__sidebar" id="letter">
			<view class="t-index-address__index" v-for="group in cityList" :key="group.initial"
			 @touchstart.stop.prevent="onTouchMove(group.initial)" @touchend.stop.prevent="onTouchStop"
				@touchmove.stop.prevent="handletouchMove($event)" @touchcancel.stop.prevent="onTouchStop">
				<span>{{ group.initial }}</span>
			</view>
		</view>

		<view class="t-index-address__alert" v-if="touchmove">
			<text>{{ activeIndex }}</text>
		</view>




	</view>
</template>

<script>
	const app = getApp();
	const util = require("@utils/util.js");
	const network = require("@manager/network-manager.js");
	export default {
		props: {
			backgroundColor: {
				type: String,
				value: '#000',
			},

		},

		data() {
			return {
				scrollview: "A",
				cityList: [],
				activeIndex: "A",
				touchmove: false,
				lastClickCode: '',
				letterTop: 0
			};
		},

		watch: {

			activeIndex(value) {
				this.scrollview = value;
			},
		},
		methods: {
			clearData() {
				this.lastClickCode = ''
			},
			getCityJson: function() {
				var uri = app.globalData.config.interfaces.URL_AREA_GETCITYJSON;
				network.get(uri, '', 'json').then(res => {
					if (res.code == 200) {
						this.cityList = res.data;
					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
					console.error(res);
				});

			},

			onTouchMove(index) {
				this.activeIndex = index;
				this.touchmove = true;
			},
			onTouchStop() {
				this.touchmove = false;
			},
			clickCity(city) {
				this.lastClickCode = city.code
				this.$emit('select', city)
			},

			handletouchMove(e) {
				var that = this
				uni.createSelectorQuery().in(this).select('#letter').boundingClientRect(function(rect) {
					that.letterTop = rect.top;
				}).exec();
				
				if (this.touchmove) {
					//函数节流
					if (this.timer) {
						clearTimeout(this.timer)
					}
					this.timer = setTimeout(() => {
						const startY = this.letterTop
						const touchY = e.touches[0].clientY
						const index = Math.floor((touchY - startY) / 22) //字母高度为 22 见 cityText_unchoose
						if (index >= 0 && index < this.cityList.length) {
							this.activeIndex = this.cityList[index].initial
						}
					}, 16)

				}

			},


		},
		mounted() {
			this.getCityJson()
		},





	};
</script>

<style lang="scss" scoped>
	.t-index-address {
		height: 100%;

		&__scroll-view {
			width: 100%;
			height: 100%;
			max-height: 100vh;
		}

		&__anchor {
			padding: 15rpx 30rpx;
			width: 100%;
			font-size: 28rpx;
			font-weight: 400;
			margin-left: 10px;
			color: #ADB0AE;
			background-color: rgb(245, 245, 245);
		}

		&__list {
			padding: 0 70rpx 0 30rpx;
		}

		&__cell {
			height: 100rpx;
			line-height: 100rpx;
			border-bottom: 1rpx solid #f2f2f2;

			&:last-child {
				border: none;
			}
		}

		&__sidebar {
			position: fixed;
			top: 50%;
			right: 0;
			transform: translateY(-50%);
			z-index: 99;
		}

		&__index {
			padding: 10rpx 20rpx;
			font-size: 22rpx;
			font-weight: 500;
			line-height: 1;
		}

		&__alert {
			position: fixed;
			top: 50%;
			right: 90rpx;
			z-index: 99;
			margin-top: -60rpx;
			width: 120rpx;
			height: 120rpx;
			font-size: 50rpx;
			color: #fff;
			border-radius: 24rpx;
			background-color: rgba(0, 0, 0, 0.5);
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}

	.new-address-layout {
		margin-top: 5px;
		margin-left: 20px;
		margin-bottom: 5px;
		margin-right: 20px;
		display: flex;
		flex-wrap: wrap;

	}

	.cityText_unchoose {
		height: 22px;
		border-radius: 4px;
		text-align: center;
		line-height: 22px;
		align-items: center;
		font-size: 16px;
		color: #3D3A3A;
		border: 1px solid #ADB0AE;
		margin: 5px;
		padding-left: 10px;
		padding-right: 10px;
		padding-top: 3px;
		padding-bottom: 3px;
		min-width: 50px;
		background-color: #f5f5f5;
		// box-sizing: border-box;
	}

	.cityText_choose {
		height: 22px;
		border-radius: 4px;
		text-align: center;
		line-height: 22px;
		align-items: center;
		font-size: 16px;
		color: #ffffff;
		border: 1px solid #EA5404;
		margin: 5px;
		padding-left: 10px;
		padding-right: 10px;
		padding-top: 3px;
		padding-bottom: 3px;
		min-width: 50px;
		background-color: #EA5404;

		// box-sizing: border-box;
	}
</style>
